package com.ygk.bluepath.member.View;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ygk.bluepath.R;
import com.ygk.bluepath.core.BFragment;
import com.ygk.bluepath.member.Presenter.RegisterPresenterFragmentThree;

import utils.ImageUtil;
import utils.Inf;

/**
 * Created by Handreace on 16-7-13.
 */
public class RegisterFragmentThree extends BFragment implements View.OnClickListener {
    private View mRootView;

    @ViewInject(R.id.et_car_number)
    private EditText et_car_number;


    @ViewInject(R.id.iv_vehicleLicense_front)
    private ImageView iv_vehicleLicense_front;
    @ViewInject(R.id.iv_vehicleLicense_back)
    private ImageView iv_vehicleLicense_back;
    @ViewInject(R.id.iv_vehicleLicense_people)
    private ImageView iv_vehicleLicense_people;

    private RegisterPresenterFragmentThree registerPresenterFragmentThree;

    private boolean isChooseFront = false;//是否选择了行驶证正本
    private boolean isChooseBack = false;//是否选择了行驶证副本
    private boolean isChoosePeople = false;//是否选择了行驶证人车合影

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_register_step3, container, false);
        ViewUtils.inject(this, mRootView);
        registerPresenterFragmentThree = new RegisterPresenterFragmentThree(this);
        initView();
        return mRootView;
    }

    private void initView() {

        // ((ImageView) mRootView.findViewById(R.id.iv_step)).setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
        mRootView.findViewById(R.id.tv_ignore).setOnClickListener(this);
        mRootView.findViewById(R.id.tv_complete).setOnClickListener(this);
        mRootView.findViewById(R.id.ll_vehicleLicense_front).setOnClickListener(this);
        mRootView.findViewById(R.id.ll_vehicleLicense_back).setOnClickListener(this);
        mRootView.findViewById(R.id.ll_vehicleLicense_people).setOnClickListener(this);

    }

    private int takePicType = 0;//选择图片的类型:默认0 行驶证正面照;1 行驶证背面照 ; 2 行驶证半身照

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_vehicleLicense_front://行驶证正面照选择
                takePicType = 0;
                registerPresenterFragmentThree.choosePic();

                break;
            case R.id.ll_vehicleLicense_back://行驶证背面照选择
                takePicType = 1;
                registerPresenterFragmentThree.choosePic();

                break;
            case R.id.ll_vehicleLicense_people://行驶证半身照选择
                takePicType = 2;
                registerPresenterFragmentThree.choosePic();

                break;

            case R.id.tv_ignore://忽略
                registerPresenterFragmentThree.ignoreSubmit(et_car_number.getText().toString());

                break;
            case R.id.tv_complete://完成
                registerPresenterFragmentThree.completeSubmit(et_car_number.getText().toString(), isChooseFront, isChooseBack, isChoosePeople);

                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Inf.TAKE_PICTURE_CAMERA: // --------------------camera

                if (resultCode == getActivity().RESULT_OK) {
                    tt("图片处理中，请稍后……");
                    setView(Inf.cameraPath);
                } else {
                    tt("照片获取失败");
                }

                break;

            case Inf.TAKE_PICTURE_ALBUMS:// ---------------------------albums
                if (data != null) {
                    if (resultCode == getActivity().RESULT_OK) {
                        tt("图片处理中，请稍后……");
                        setView(ImageUtil.getRealFilePath(getActivity(),
                                data.getData()));
                       /* addInserPicFirst(
                                UiOperateUtil.getRealFilePath(ACHRelease.this,
                                        data.getData()), false);*/
                    } else {
                        tt("照片获取失败");
                    }
                }

                break;
        }
    }


    private void setView(final String path) {

        switch (takePicType) {//选择图片的类型:默认0 行驶证正面照;1 行驶证背面照 ; 2 行驶证半身照x
            case 0:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ImageUtil.savePicfileWithMaxSize(path, "vehicleLicense_front.jpg", getPhotos_cache(), 200);
                        getActivity().runOnUiThread(new Runnable() {
                            public void run() {
                                disPlayImage("file:///" + path, iv_vehicleLicense_front);
                                isChooseFront = true;
                                ((RegisterActivity) getActivity()).setVehicleLicenseFrontUrl(getPhotos_cache() + "vehicleLicense_front.jpg");
                            }
                        });
                    }
                }).start();
                break;
            case 1:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ImageUtil.savePicfileWithMaxSize(path, "vehicleLicense_back.jpg", getPhotos_cache(), 200);
                        getActivity().runOnUiThread(new Runnable() {
                            public void run() {
                                disPlayImage("file:///" + path, iv_vehicleLicense_back);
                                isChooseBack = true;
                                ((RegisterActivity) getActivity()).setVehicleLicenseBackUrl(getPhotos_cache() + "vehicleLicense_back.jpg");
                            }
                        });
                    }
                }).start();
                break;
            case 2:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ImageUtil.savePicfileWithMaxSize(path, "vehicleLicense_people.jpg", getPhotos_cache(), 200);
                        getActivity().runOnUiThread(new Runnable() {
                            public void run() {
                                disPlayImage("file:///" + path, iv_vehicleLicense_people);
                                isChoosePeople = true;
                                ((RegisterActivity) getActivity()).setVehicleLicensePeopleUrl(getPhotos_cache() + "vehicleLicense_people.jpg");
                            }
                        });
                    }
                }).start();
                break;
        }
    }

}
